package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.WebVisitMapper;
import com.itheima.pojo.Comment;
import com.itheima.pojo.User;
import com.itheima.pojo.WebVisit;
import com.itheima.service.WebVisitService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.Map;

@Service
public class WebVisitServiceImpl implements WebVisitService {

    @Autowired
    private WebVisitMapper webVisitMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public Page<WebVisit> findAll(Map<String, Object> webVisitVO) {
        Integer currentPage = Integer.parseInt(webVisitVO.get("currentPage").toString());
        Integer pageSize = Integer.parseInt(webVisitVO.get("pageSize").toString());
        PageHelper.startPage(currentPage,pageSize);
        Example example=this.createExample(webVisitVO);
        Page<WebVisit> webVisitPage = (Page<WebVisit>) webVisitMapper.selectByExample(example);
        return webVisitPage;
    }

    @Override
    public void add(User user) {
        WebVisit webVisit =new WebVisit();
        webVisit.setUid(String.valueOf(idWorker.nextId()));
        webVisit.setBehavior("用户登陆");
        webVisit.setCreateTime(new Date());
        webVisit.setBrowser(user.getBrowser());
        webVisit.setIp(user.getIpSource());
        webVisit.setOs(user.getOs());
        webVisit.setUpdateTime(new Date());
        webVisit.setUserUid(user.getUid());
        webVisitMapper.insert(webVisit);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(WebVisit.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 唯一uid
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andLike("uid","%"+searchMap.get("uid")+"%");
            }
            // 用户uid
            if(searchMap.get("userUid")!=null && !"".equals(searchMap.get("userUid"))){
                criteria.andLike("userUid","%"+searchMap.get("userUid")+"%");
            }
            // 访问ip地址
            if(searchMap.get("ip")!=null && !"".equals(searchMap.get("ip"))){
                criteria.andLike("ip","%"+searchMap.get("ip")+"%");
            }

            // 用户行为
            if(searchMap.get("behavior")!=null && !"".equals(searchMap.get("behavior"))){
                criteria.andEqualTo("behavior",searchMap.get("behavior"));
            }

            // 模块uid（文章uid，标签uid，分类uid）
            if(searchMap.get("moduleUid")!=null && !"".equals(searchMap.get("moduleUid"))){
                criteria.andEqualTo("moduleUid",searchMap.get("moduleUid"));
            }

            // 附加数据(比如搜索内容)
            if(searchMap.get("otherData")!=null && !"".equals(searchMap.get("otherData"))){
                criteria.andEqualTo("otherData",searchMap.get("otherData"));
            }

            // 状态
            if(searchMap.get("status")!=null){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

            // 创建时间
            if(searchMap.get("createTime")!=null){
                criteria.andEqualTo("createTime",searchMap.get("createTime"));
            }

            // 更新时间
            if(searchMap.get("updateTime")!=null){
                criteria.andEqualTo("updateTime",searchMap.get("updateTime"));
            }

            // 操作系统
            if(searchMap.get("os")!=null && !"".equals(searchMap.get("os"))){
                criteria.andEqualTo("os",searchMap.get("os"));
            }

            // 浏览器
            if(searchMap.get("browser")!=null && !"".equals(searchMap.get("browser"))){
                criteria.andEqualTo("browser",searchMap.get("browser"));
            }

            // ip来源
            if(searchMap.get("ipSource")!=null && !"".equals(searchMap.get("ipSource"))){
                criteria.andEqualTo("ipSource",searchMap.get("ipSource"));
            }

        }
        return example;
    }
}
